//
//  XbnOrderRefundChooseView.m
//  XbnLcBuyer
//
//  Created by xbn_iMac on 15/8/7.
//  Copyright (c) 2015年 ZhaoDongbo. All rights reserved.
//

#import "LZActionSheet.h"
#import "Masonry.h"

@interface LZActionSheet ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *arrData;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation LZActionSheet

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.bgView = [[UIView alloc] initWithFrame:self.bounds];
        self.bgView.backgroundColor = UIColorFromRGB(0x000000,1);
        self.bgView.alpha = 0;
        [self addSubview:self.bgView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(HideView)];
        tap.numberOfTapsRequired = 1;
        [self.bgView addGestureRecognizer:tap];
        
        [self creatTable];
        self.hidden = YES;
    }
    return self;
}

// 创建标题view
-(void)creatHeaderWithTitle:(NSString*)title andHeight:(CGFloat)height
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_table.frame), height)];
    view.backgroundColor = [UIColor whiteColor];
    self.table.tableHeaderView = view;
    
    // 标题label
    UILabel *lab = [[UILabel alloc] init];
    lab.text = title;
    lab.numberOfLines = 0;
    lab.textAlignment = NSTextAlignmentCenter;
    lab.backgroundColor = [UIColor clearColor];
    lab.textColor = UIColorFromRGB(0x999999,1);
    lab.font = [UIFont systemFontOfSize:12];
    [view addSubview:lab];
    
    // 线
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:203./255. green:204./255. blue:205./255. alpha:1];
    [view addSubview:line];
    
    
    // vfl
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.centerX.and.centerY.equalTo(view);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
    }];
}

-(void)creatTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(5, 0, CGRectGetWidth(self.frame)-10, 0) style:UITableViewStylePlain];
    self.table.scrollEnabled = NO;
    self.table.layer.cornerRadius = 3;
    self.table.rowHeight = 50;
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.backgroundColor = [UIColor clearColor];
    self.table.backgroundView = nil;
    self.table.separatorColor = [UIColor colorWithRed:203.0/255.0 green:204.0/255.0 blue:205.0/255.0 alpha:1.0];
    self.table.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)+CGRectGetHeight(self.table.frame)/2.0);
    [self addSubview:self.table];
    
    if ([self.table respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.table setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.table respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.table setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - 刷新数据

-(void)ReloadTableViewArrayTitle:(NSArray*)arrTitle
{
    self.arrData = arrTitle;
    CGRect rect = self.table.frame;
    float rowHight = [self getTotleRowHeight];
    rect.size.height = rowHight+10*(self.arrData.count-1)+(self.table.tableHeaderView?self.table.tableHeaderView.frame.size.height:0);
    self.table.frame = rect;
    [self.table reloadData];
}

-(float)getTotleRowHeight {
    float rowHight = 0.0;
    for (int i = 0; i<self.arrData.count; i++) {
        NSArray *arrSubs = self.arrData[i];
        for (int j = 0; j<arrSubs.count; j++) {
            rowHight += 50;
        }
    }
    return rowHight;
}

#pragma mark - 显示UI

-(void)ShowView
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.alpha = 0.7;
        self.table.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)-CGRectGetHeight(self.table.frame)/2.0-10);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - 隐藏UI

-(void)HideView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.alpha = 0;
        self.table.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)+CGRectGetHeight(self.table.frame)/2.0);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSArray *arrRows = self.arrData[section];
    return arrRows.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.001;
    }
    return 10.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v  =[[UIView alloc] init];
    return v;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellIdentifier = @"chooseviewcell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = UIColorFromRGB(0xff7a21,1);
    cell.textLabel.text = self.arrData[indexPath.section][indexPath.row];
    
    if (self.table.tableHeaderView == nil) {
        // 如果没有headerview
        NSArray *subArray = self.arrData[indexPath.section];
        if (subArray.count == 0) {
            // 如果该列只有一个元素，则是全圆角
            [self cellRoundCornerCellTypeSingleRow:cell];
        }
        else
        {
            // 如果该列大于一个元素
            if (indexPath.row == 0) {
                // 如果是第一个元素，则山上边圆角，下边直角
                [self cellRoundCornerCellTypeTop:cell];
            }
            else if (indexPath.row == subArray.count-1)
            {
                // 如果是该列的最后一个元素，则上边直角，下边圆角
                [self cellRoundCornerCellTypeBottom:cell];
            }
        }
    }
    else
    {
        // 如果有headerview
        if (self.arrData.count>1) {
            NSArray *subArray = self.arrData[indexPath.section];
            if (indexPath.section == 0 && indexPath.row == subArray.count-1) {
                // 如果是第一列的最后一个，则上边直角，下边圆角
                [self cellRoundCornerCellTypeBottom:cell];
            }
            else if (indexPath.section >0) {
                // 如果是第一列之后的
                if (subArray.count == 0) {
                    // 如果该列只有一个元素，则是全圆角
                    [self cellRoundCornerCellTypeSingleRow:cell];
                }
                else
                {
                    // 如果该列大于一个元素
                    if (indexPath.row == 0) {
                        // 如果是第一个元素，则山上边圆角，下边直角
                        [self cellRoundCornerCellTypeTop:cell];
                    }
                    else if (indexPath.row == subArray.count-1)
                    {
                        // 如果是该列的最后一个元素，则上边直角，下边圆角
                        [self cellRoundCornerCellTypeBottom:cell];
                    }
                }
            }
        }
    }
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.ClickSheetBlock) {
        [self HideView];
        UITableViewCell * cell = [self.table cellForRowAtIndexPath:indexPath];
        self.ClickSheetBlock(indexPath,cell.textLabel.text);
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.section == 0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
}

/**
 *  @author LY, 16-04-11 17:04:19
 *
 *  上边圆角，下边直角
 *
 */
-(void)cellRoundCornerCellTypeTop:(UITableViewCell*)cell {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.frame)-10, 50) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;
    cell.layer.mask = maskLayer;
}

/**
 *  @author LY, 16-04-11 17:04:59
 *
 *  上边直角，下边圆角
 *
 *  @param cell 需要画的cell对象
 */
-(void)cellRoundCornerCellTypeBottom:(UITableViewCell*)cell {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.frame)-10, 50) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;
    cell.layer.mask = maskLayer;
}

/**
 *  @author LY, 16-04-11 17:04:41
 *
 *  全圆角
 *
 *  @param cell 需要画的cell对象
 */
-(void)cellRoundCornerCellTypeSingleRow:(UITableViewCell*)cell {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.frame)-10, 50) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;
    cell.layer.mask = maskLayer;
}

@end
